﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : Lolo
// Created          : 09-21-2013
//
// Last Modified By : Lolo
// Last Modified On : 09-23-2013
// ***********************************************************************
// <copyright file="LogOffController.cs" company="">
//     Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using InnovaApp.Layers.SecurityLayer.Models.AccountModels.MembershipServices;
using INNOVACALL.Framework.Common.MVCHelpers;
using INNOVACALL.Framework.Common.MVCHelpers.Localization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace InnovaApp.Portals.MVC4Portal.Controllers.ViewControllers.LayoutViewControllers.SecurityControllers
{
    /// <summary>
    /// Class LogOffController.
    /// </summary>
    public class LogOffController : Controller
    {
        /// <summary>
        /// Gets or sets the forms service.
        /// </summary>
        /// <value>The forms service.</value>
        public IFormsAuthenticationService FormsService { get; set; }

        /// <summary>
        /// Initialise des données qui peuvent ne pas être disponibles lorsque le constructeur est appelé.
        /// </summary>
        /// <param name="requestContext">Contexte HTTP et données d'itinéraire.</param>
        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            base.Initialize(requestContext);
        }

        /// <summary>
        /// GET: /LogOff/
        /// </summary>
        /// <returns>ActionResult.</returns>
        [CacheManager(enCacheMode.NoCache)]
        public ActionResult Index()
        {
            FormsService.SignOut();
            Session.Abandon();

            // clear authentication cookie
            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            cookie1.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie1);

            // clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
            HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            cookie2.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie2);

            // clear Favorite language cookie
            HttpCookie cookie3 = new HttpCookie(INNOVACALL.Framework.Common.MVCHelpers.Localization.CommonLocalization.FavoriteLanguageCookieName, "");
            cookie3.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie3);

            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();

            return RedirectPermanent("~/");
        }

    }
}
